package cn.com.huzo.service.trade.impl;

import cn.com.huzo.model.entities.trade.ShippingOrderHeaderEntity;
import huzofw.model.entities.paginator.Paginator;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.com.huzo.repository.trade.ShippingOrderHeaderRepository;
import cn.com.huzo.repository.trade.ShippingOrderLinesDoRepository;
import cn.com.huzo.repository.trade.ShippingOrderLinesRepository;
import cn.com.huzo.service.trade.ShippingOrderHeaderManager;
import cn.com.huzo.service.trade.ShippingOrderLinesDoManager;
import cn.com.huzo.service.trade.ShippingOrderLinesManager;
import org.apache.commons.collections4.MapUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发货单管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("ShippingOrderHeaderManager")
public class ShippingOrderHeaderManagerImpl extends GenericManagerImpl<ShippingOrderHeaderEntity, Long>
        implements ShippingOrderHeaderManager {

    @Resource(name = "ShippingOrderHeaderRepository")
    private ShippingOrderHeaderRepository _dao_header;

    @Resource(name = "ShippingOrderLinesRepository")
    private ShippingOrderLinesRepository _dao_lines;

    @Resource(name = "ShippingOrderLinesDoRepository")
    private ShippingOrderLinesDoRepository _dao_lines_do;

    @Resource(name = "ShippingOrderLinesDoManager")
    private ShippingOrderLinesDoManager _manager_lines_do;

    @Resource(name = "ShippingOrderLinesManager")
    private ShippingOrderLinesManager _manager_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchByHeaderId(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        _result.put("HeaderEntity", _dao_header.fetchByKey(MapUtils.getLong(criterion, "HeaderId")));

        Map<String, Object> params = new HashMap();
        params.put("HeaderId", MapUtils.getLong(criterion, "HeaderId"));
        params.put("PageIndex", MapUtils.getInteger(criterion, "PageIndex01"));
        params.put("Limit", MapUtils.getInteger(criterion, "Limit01"));
        _result.put("LinesDoList", _manager_lines_do.fetchForPage(params));

        params.clear();
        params.put("HeaderId", MapUtils.getLong(criterion, "HeaderId"));
        params.put("PageIndex", MapUtils.getInteger(criterion, "PageIndex02"));
        params.put("Limit", MapUtils.getInteger(criterion, "Limit02"));
        _result.put("LinesList", _manager_lines.fetchForPage(params));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public Map<String, Object> fetchPrintData(Long headerId) throws Exception {
        Map<String, Object> _result = new HashMap();

        _result.put("HeaderEntity", _dao_header.fetchByKey(headerId));
        _result.put("SizeNameNode", _dao_lines.fetchSizeNameNode(headerId));
        _result.put("LinesList", _dao_lines.fetchPrintData(headerId));
        _result.put("LinesDescList", _dao_lines.fetchLinesDesc(headerId));
        _result.put("SumQuantity", _dao_lines_do.fetchSumQuantityByHeaderId(headerId));
        _result.put("CartonAmount", _dao_lines.fetchCartonAmount(headerId));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public Map<String, Object> fetchPrintData02(Long headerId) throws Exception {
        Map<String, Object> _result = new HashMap();

        _result.put("HeaderEntity", _dao_header.fetchByKey(headerId));
        _result.put("SizeNameNode", _dao_lines.fetchSizeNameNode(headerId));
        _result.put("LinesList", _dao_lines.fetchPrintData02(headerId));
        _result.put("LinesDescList", _dao_lines.fetchLinesDesc(headerId));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public Map<String, Object> fetchPrintData03(Long headerId) throws Exception {
        Map<String, Object> _result = new HashMap();

        _result.put("HeaderEntity", _dao_header.fetchByKey(headerId));
        _result.put("SizeNameNode", _dao_lines.fetchSizeNameNode(headerId));
        _result.put("LinesList", _dao_lines.fetchPrintData03(headerId));
        _result.put("LinesDescList", _dao_lines.fetchLinesDesc(headerId));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public String fetchNewBillNo() throws Exception {
        return _dao_header.fetchNewBillNo();
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        Long offset = (long) ((Integer) criterion.get("Limit")) * (((Integer) criterion.get("PageIndex")) - 1);
        long total = _dao_header.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<ShippingOrderHeaderEntity> itemList = _dao_header.fetchForPage(criterion);
            Paginator<ShippingOrderHeaderEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            _result.put("TotalRecordCount", paginator.getTotal());
            _result.put("TotalPages", paginator.getTotalPages());
            _result.put("PageIndex", paginator.getPageIndex());
            _result.put("DataList", paginator.getItemList());
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     */
    @Transactional
    @Override
    public Long insert(ShippingOrderHeaderEntity instance) throws Exception {
        Long key = _dao_header.insert(instance);
        if (key > 0) {
            // 审计日志
            //writeAuditLog(instance.getUpdatedBy(), "贸易信息→发货单", "单据号：" + instance.getBillNo(), "增加");
        }

        return key;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer update(ShippingOrderHeaderEntity instance) throws Exception {
        Integer rowsAffected = _dao_header.update(instance);
        if (rowsAffected > 0) {
            // 审计日志
            //writeAuditLog(instance.getUpdatedBy(), "贸易信息→发货单", "单据号：" + instance.getBillNo(), "更新");
        }

        return rowsAffected;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        // 删除
        Integer rowsAffected = _dao_header.delete(MapUtils.getLong(criterion, "HeaderId"));
        if (rowsAffected > 0) {
            // 审计日志
            //writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"), "贸易信息→发货单", "单据号：" + MapUtils.getString(criterion, "BillNo"), "删除");
        }
        return fetchForPage(criterion);
    }
}
